/*
 * OpenParts
 * A dynamic-loading components framework for GWT
 * Copyright (C) 2011 Christophe Bouthier  [chris{AT}binary-gastronome{DOT}fr]
 *
 * This work is partially based on work I have done at INRIA (http://www.inria.fr) 
 * in the context of the Qualipso European Project (http://qualipso.org/),
 * The work done at INRIA is Copyright (C) 2006-2011 INRIA
 *
 * This work is distributed under the LGPL version 3
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * as published by the Free Software Foundation. See the GNU
 * Lesser General Public License in LGPL.txt for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package fr.openparts.OpenParts.test.gwt.tests;

import com.google.gwt.junit.client.GWTTestCase;

import fr.openparts.OpenParts.client.OPBinding;
import fr.openparts.OpenParts.client.OPPartLoadingRequest;
import fr.openparts.OpenParts.client.OPPartsLoaderBinding;
import fr.openparts.OpenParts.client.OPShell;

/**
 * GWT tests for the parts loader of OPBinding.
 * 
 * @author Christophe Bouthier [chris{AT}binary-gastronome{DOT}fr]
 * @creation.date 25 January 2010
 */
public class OPSharedBindingPartsLoaderTestGwt extends GWTTestCase {

    private OPBinding binding;

    /**
     * Return the name of the tested GWT Module.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#getModuleName()
     */
    @Override
    public final String getModuleName() {
        return "fr.openparts.OpenParts.test.gwt.TestOpenParts";
    }

    /**
     * Called before each test, set up the binding.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#gwtSetUp()
     */
    @Override
    protected final void gwtSetUp() throws Exception {
        super.gwtSetUp();
        binding = OPShell.opBindingSharedInstance();
    }

    /**
     * Called at the end of each test to clean up the mess.
     * 
     * @see com.google.gwt.junit.client.GWTTestCase#gwtTearDown()
     */
    @Override
    protected final void gwtTearDown() throws Exception {
        super.gwtTearDown();
        unbindOPPartsLoader(OPShell.opBindingSharedInstance());
    }

    /**
     * Test binding a parts loader. Should get something not null.
     */
    public final void testBindingPartsLoader() {
        binding.bind(OPPartsLoaderBinding.partsLoaderBindingFactory());
        assertNotNull("parts loader shouldn't be null", getPartsLoader(binding));
    }

    /**
     * Test binding a null parts loader. Should raise an assertion error.
     */
    public final void testBindingNullPartsLoader() {
        if (assertionsAreEnabled()) {
            try {
                binding.bind((OPPartsLoaderBinding) null);
                fail("OPBinding should raise an exception when binding a null parts loader.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }
    
    /**
     * Test trying to execute a null loading request. Should raise an assertion error.
     */
    public final void testExecutingNullLoadingRequest() {
        if (assertionsAreEnabled()) {
            try {
                binding.executeLoadingRequest(null);
                fail("OPBinding should raise an exception when executing a null loading request.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }
    
    /**
     * Test trying to execute a loading request without a parts loader. Should raise an assertion error.
     */
    public final void testExecutingLoadingRequestWithoutPartsLoader() {
        if (assertionsAreEnabled()) {
            try {
                binding.executeLoadingRequest(OPPartLoadingRequest.partLoadingRequestFactory("dummyService", null));
                fail("OPBinding should raise an exception when executing a null loading request.");
            } catch (AssertionError e) {
                assertTrue("Error should be an AssertionError", e instanceof AssertionError);
            }
        }
    }

    /**
     * Returns the parts loader of the binding.
     * 
     * @param binding
     *            the binding
     * @return the parts loader
     */
    private final native OPPartsLoaderBinding getPartsLoader(OPBinding binding) /*-{
        return binding.partsLoader;
    }-*/;

    /**
     * Unbind the OPPartsLoader.
     */
    private final native void unbindOPPartsLoader(final OPBinding binding) /*-{
        binding.partsLoader = null;
    }-*/;

    /**
     * Check if assertions are enabled.
     * 
     * @return true if assertions are enabled, false otherwise
     */
    private final boolean assertionsAreEnabled() {
        try {
            assert false;
            return false;
        } catch (AssertionError e) {
            return true;
        }
    }
}
